<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace app\api\controller\v1\order;

use app\api\controller\BaseController;
use app\api\validate\v1\order\OrderValidate;
use app\Request;
use app\services\order\OrderService;
use crmeb\exceptions\ApiException;
use think\facade\Config;

/**
 * 订单相关
 */
class OrderController extends BaseController
{
    protected $service;

    public function __construct()
    {
        $this->service = new OrderService();
    }

    /**
     * 确认订单详情
     */
    public function info(Request $request)
    {
        $params = $request->getMore([
            ['cartId', []],
            ['coupon_id',0],
            ['address_id', 0]
        ]);
        $params['cart_ids'] = $params['cartId'];
        $params['user_id'] = $request->uid();
        $this->baseValidate((new OrderValidate()), $request->action(), $params);

        $result = $this->service->info($params);

        return app('json')->success($result);
    }

    /**
     * 创建订单
     */
    public function create(Request $request)
    {
        $params = $request->postMore([
            ['address_id', ''],
            ['cart_ids', []],
            ['coupon_id', 0],
            ['remark', ''],
            ['fact_price', ''],
        ]);
        $user = $request->user()->toArray();
        $params['user_id'] = $user['uid'];
        $this->baseValidate((new OrderValidate()), $request->action(), $params);

        $order_id = $this->service->create($params);


        return ($order_id) ? app('json')->success(['order_id' => $order_id]) : new ApiException(100005);
    }

    /**
     * 再次下单
     */
    public function again(Request $request)
    {
        $params = $request->postMore([
            ['order_id', ''],
        ]);
        $params['user_id'] = $request->uid();
        $this->baseValidate((new OrderValidate()), $request->action(), $params);
        $this->service->again($params);

        return app('json')->success(100010);
    }

    /**
     * 获取支付方式
     * @param Request $request
     * @return \think\Response
     */
    public function payList(Request $request){

        return app('json')->success(['list' => Config::get('pay.payList')]);
    }

    /**
     * 订单支付
     */
    public function pay(Request $request)
    {
        $params = $request->postMore([
            ['order_id', ''],
            ['bank_code','']
        ]);
        $params['user_id'] = $request->uid();
        $this->baseValidate((new OrderValidate()), $request->action(), $params);
        $result = $this->service->pay($params);

        return app('json')->success(['payment_url' => $result['payment_url']]);
    }

    /**
     * 订单列表
     */
    public function index(Request $request)
    {
        $params = $request->getMore([
            ['status', '']
        ]);
        $params['user_id'] = $request->uid();

        $result = $this->service->index($params);

        return app('json')->success($result);
    }

    /**
     * 订单详情
     */
    public function detail(Request $request)
    {
        $params = $request->getMore([
            ['order_id', '']
        ]);
        $params['uid'] = $request->uid();
        $this->baseValidate((new OrderValidate()), $request->action(), $params);

        $result = $this->service->detail($params);

        return app('json')->success($result);
    }

    /**
     * 查看物流
     */
    public function viewLogistics(Request $request)
    {
        $params = $request->getMore([
            ['order_id', '']
        ]);
        $this->baseValidate((new OrderValidate()), $request->action(), $params);

        $result = $this->service->viewLogistics($params);

        return app('json')->success($result);
    }

    /**
     * 确认收货
     */
    public function confirmReceipt(Request $request)
    {
        $params = $request->postMore([
            ['order_id', '']
        ]);
        $this->baseValidate((new OrderValidate()), $request->action(), $params);

        $this->service->confirmReceipt($params);

        return app('json')->success(100010);
    }

    /**
     * 取消订单
     */
    public function cancel(Request $request)
    {
        $params = $request->postMore([
            ['order_id', ''],
        ]);
        $params['user_id'] = $request->uid();
        $this->service->cancel($params);

        return app('json')->success(100010);
    }

    /**
     * 删除订单
     */
    public function delete(Request $request)
    {
        $params = $request->postMore([
            ['order_id', ''],
        ]);
        $params['user_id'] = $request->uid();
        $this->baseValidate((new OrderValidate()), $request->action(), $params);
        $this->service->delete($params);

        return app('json')->success(100010);
    }

    /**
     * 查询订单是否支付成功
     * @param Request $request
     */
    public function query(Request $request){
        $params = $request->getMore([
            ['order_id', ''],
        ]);

        $params['user_id'] = $request->uid();
        $this->baseValidate((new OrderValidate()), $request->action(), $params);
        $result = $this->service->queryOrder($params);

        return app('json')->success(['pay_status' => $result['pay_status']]);
    }

}